package com.xiaoshuidi.cloud.module.member.controller.app.point;

import com.xiaoshuidi.cloud.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.module.member.controller.admin.point.vo.MemberPointsChangeVo;
import com.xiaoshuidi.cloud.module.member.controller.app.point.vo.LoginInfo;
import com.xiaoshuidi.cloud.module.member.controller.app.point.vo.MallEncodeResp;
import com.xiaoshuidi.cloud.module.member.controller.app.point.vo.MallPointRecord;
import com.xiaoshuidi.cloud.module.member.controller.app.point.vo.PointRefund;
import com.xiaoshuidi.cloud.module.member.controller.app.point.vo.SmsCodeMallReqDTO;
import com.xiaoshuidi.cloud.module.member.controller.app.point.vo.UserPointInfo;
import com.xiaoshuidi.cloud.module.member.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.member.service.point.MallEncodeService;
import com.xiaoshuidi.cloud.module.member.service.point.MemberPointsRecordService;
import com.xiaoshuidi.cloud.module.member.service.point.MemberPointsService;
import com.xiaoshuidi.cloud.module.member.service.user.MemberUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;

import java.util.List;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;

@Tag(name = "租客APP - 积分商城")
@Slf4j
@RestController
@RequestMapping(ApiConstants.NAME + "/points-mall")
public class PointMallController {

    @Resource
    private MemberUserService userService;

    @Resource
    MemberPointsService tenantPointsService;

    @Resource
    MemberPointsRecordService tenantPointsRecordService;

    @Resource
    MallEncodeService mallEncodeService;

    @PostMapping("/get-encode-token")
    @Operation(summary = "获得登录信息加密信息和签文")
    public CommonResult<MallEncodeResp> getEncodeToken(@RequestBody LoginInfo loginInfo) throws Exception {
         Long tenantId = SecurityFrameworkUtils.getLoginUser().getTenantId();
         loginInfo.setTenantId(tenantId);
        return success(mallEncodeService.getEncodeToken(loginInfo));
    }


    @GetMapping("/get-point-by-id/{userId}")
    @Operation(summary = "获得用户积分信息")
    public CommonResult<UserPointInfo> getUserPointInfo(@PathVariable("userId") Long id) {

        return success(userService.getUserPointInfo(id));
    }

    @PostMapping("/modify-point")
    @Operation(summary = "增加或者扣减积分")
    public CommonResult<Boolean> modifyTenantPoints(@RequestBody MemberPointsChangeVo reqVo) {
        Boolean flag = tenantPointsService.modifyTenantPoints(reqVo);
        if (flag) {
            return CommonResult.success(flag);
        } else {
            return CommonResult.error(GlobalErrorCodeConstants.BAD_REQUEST.getCode(), GlobalErrorCodeConstants.BAD_REQUEST.getMsg());
        }

    }

    @PostMapping("/refund")
    @Operation(summary = "积分退费")
    @PermitAll
    public CommonResult<Boolean> refundTenantPoints(@RequestBody PointRefund reqVo) {
        Boolean flag = tenantPointsService.refundTenantPoints(reqVo);
        if (flag) {
            return CommonResult.success(flag);
        } else {
            return CommonResult.error(GlobalErrorCodeConstants.BAD_REQUEST.getCode(), GlobalErrorCodeConstants.BAD_REQUEST.getMsg());
        }
    }


    @GetMapping("/get-change-detail/{userId}")
    @Operation(summary = "积分变动分页列表")
    public CommonResult<PageResult<MallPointRecord>> getMallPointsChangeDetail(@PathVariable("userId") Long id, @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo, @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        PageResult<MallPointRecord> page = tenantPointsRecordService.getMallPointsChangeDetail(id, pageNo, pageSize);
        return CommonResult.success(page);
    }


    @GetMapping("/send-mail-code")
    @Operation(summary = "发送积分商城验证码")
    public CommonResult<Boolean> sendMailCode(@RequestParam(value = "mobile", required = true) String mobile) {
        tenantPointsService.sendMailCode(mobile);
        return success(true);
    }


    @PostMapping("/validate-mail-code")
    @Operation(summary = "校验积分商城验证码")
    public CommonResult<Boolean> validateMailCode(@RequestBody @Valid SmsCodeMallReqDTO reqVO) {
        tenantPointsService.validateMailCode(reqVO);
        return success(true);
    }
}
